﻿using System;
using System.Collections.Generic;

using System.Text;
using MVCFoto.IDAL;
using System.Data.SqlClient;
using System.Data;
using MVCFoto.Model;

namespace MVCFoto.SQLServerDAL
{
    public class Comment : IComment
    {

        public int AddComment(int photoId, int userId, string userName, string content)
        {
            SqlParameter[] param = {
			SqlHelper.MakeInParam("@PhotoId",SqlDbType.Int,4,photoId),	
			SqlHelper.MakeInParam("@UserId",SqlDbType.Int,4,userId),	
            SqlHelper.MakeInParam("@UserName",SqlDbType.VarChar,50,userName),	
            SqlHelper.MakeInParam("@Content",SqlDbType.VarChar,1024,content),	
			SqlHelper.MakeInParam("@CreateDate",SqlDbType.DateTime,0,DateTime.Now)	
	        };
            return SqlHelper.ExecuteNonQuery("mfp_Insert_Comment", param);
        }
        public bool DeleteComment(int photoId, int commentId)
        {
            SqlParameter[] param = {
			SqlHelper.MakeInParam("@PhotoId",SqlDbType.Int,4,photoId),	
			SqlHelper.MakeInParam("@CommentId",SqlDbType.Int,4,commentId),	
          
	        };
            return SqlHelper.ExecuteNonQuery("mfp_Delete_Comment", param) > 0 ? true : false;
        }
        public IList<CommentInfo> GetPhotoComments(int photoId)
        {
            SqlParameter[] param = {
			SqlHelper.MakeInParam("@PhotoId",SqlDbType.Int,4,photoId)        
	        };
            IList<CommentInfo> comments = new List<CommentInfo>();
            using (SqlDataReader sdr = SqlHelper.ExecuteReader("mfp_Get_Photo_Comments", param))
            {
                while (sdr.Read())
                {
                    CommentInfo ci = new CommentInfo();
                    ci.CommentID = sdr.GetInt32(0);
                    ci.PhotoID = sdr.IsDBNull(1) ? 0 : sdr.GetInt32(1);
                    ci.UserId = sdr.IsDBNull(2) ? 0 : sdr.GetInt32(2);
                    ci.UserName = sdr.GetString(3);
                    ci.Content = sdr.GetString(4);
                    ci.CreateDate = sdr.IsDBNull(5) ? DateTime.Now : sdr.GetDateTime(5);
                    ci.UserAvatar = sdr.IsDBNull(6) ? "": sdr.GetString(6);
                    comments.Add(ci);
                }

            }
            return comments;
        }
    }
}
